//
//  OpenLotteryRecordVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/24.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "OpenLotteryRecordVC.h"
#import "OpenLotteryRecordCell.h"
#import "MJRefresh.h"
#import "QXMaskView.h"

@interface OpenLotteryRecordVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (nonatomic ,strong) NSArray        *rightArr;
@property (nonatomic ,strong) UIView         *dataPickerView;

@property (nonatomic ,assign) NSInteger   page;

@end

@implementation OpenLotteryRecordVC{
    
    TimeModel   *selectModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(onclickScreening)];
//    self.navigationItem.rightBarButtonItem=rightItem;
    // Do any additional setup after loading the view.
}

- (void)setLType:(NSString *)lType{
    self.page=1;
    _lType=lType;
    WS(weakSelf);
    
    [SVProgressHUD show];
    [[InformationVM manage] getRecordDateWithId:self.lType andSuccess:^(id returnValue) {
         [SVProgressHUD dismiss];
        weakSelf.rightArr=returnValue;
        if (weakSelf.rightArr.count>0) {
            TimeModel *model=weakSelf.rightArr[0];
            selectModel=model;
            [weakSelf.tableView.header beginRefreshing];
        }
        
       
        
    } andFail:^(NSString *errorMessage, NSError *error) {
        
       
    }];
}

- (void)onclickScreening{
    if (self.dataPickerView) {
        [self.dataPickerView removeFromSuperview];
        self.dataPickerView=nil;
        return;
    }
    
    self.dataPickerView=[[UIView alloc]init];
    self.dataPickerView.backgroundColor=DEFAULTBGCOLOR;
    [self.view addSubview:self.dataPickerView];
    [self.dataPickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(@(real_size(250)));
    }];
    
    UIDatePicker *dataPicker=[[UIDatePicker alloc]init];
    dataPicker.backgroundColor=[UIColor whiteColor];
    [dataPicker setDatePickerMode:UIDatePickerModeDate];
    [dataPicker setMaximumDate:[NSDate date]];
    [dataPicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    [_dataPickerView addSubview:dataPicker];
    [dataPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(@(real_size(210)));
    }];
    
    UIButton *cancle=[[UIButton alloc]init];
    [cancle setTitleColor:DEFAULTTEXTBLACKCOLOR forState:UIControlStateNormal];
    [cancle addTarget:self action:@selector(onclickScreening) forControlEvents:UIControlEventTouchUpInside];
    [cancle setTitle:@"取消" forState:UIControlStateNormal];
    [_dataPickerView addSubview:cancle];
    [cancle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(_dataPickerView);
        make.size.mas_equalTo(CGSizeMake(real_size(80), real_size(40)));
    }];
    
    
    UIButton *sure=[[UIButton alloc]init];
    [sure addTarget:self action:@selector(onclickSelectTime) forControlEvents:UIControlEventTouchUpInside];
    [sure setTitle:@"确定" forState:UIControlStateNormal];
    [sure setTitleColor:DEFAULTTEXTBLACKCOLOR forState:UIControlStateNormal];
    [_dataPickerView addSubview:sure];
    [sure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(_dataPickerView);
        make.size.mas_equalTo(CGSizeMake(real_size(80), real_size(40)));
    }];
}

- (void)onclickSelectTime{
    [self onclickScreening];
    [self.tableView.header beginRefreshing];
}

- (void)dateChange:(UIDatePicker *)date
{
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    selectModel.Key=[formatter stringFromDate:date.date];
}

- (void)getData{
    WS(weakSelf);
    [[InformationVM manage] getRecordListWithId:self.lType andDate:selectModel.Key andPageIndex:weakSelf.page Success:^(id returnValue) {
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView reloadData];
        NSArray *arr=returnValue;
        if (arr.count<[defaultPageSize integerValue]) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        weakSelf.page+=1;
        [weakSelf.tableView.header endRefreshing];
    } andFail:^(NSString *errorMessage, NSError *error) {
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
    }];
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OpenLotteryRecordCell *cell=[tableView dequeueReusableCellWithIdentifier:@"OpenLotteryRecordCell" forIndexPath:indexPath];
    cell.lotteryType=self.lotteryType;
    cell.model=self.dataArr[indexPath.row];
    return cell;
}







#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        [_tableView registerClass:[OpenLotteryRecordCell class] forCellReuseIdentifier:@"OpenLotteryRecordCell"];
        _tableView.estimatedRowHeight=200.f;
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.page=1;
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            [weakSelf getData];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getData];
        }];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        
    }
    return _tableView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (UIButton *)rightItemView:(NSString *)title{
    CGSize size=[TGTools boundingRectWithString:title withSize:CGSizeMake(200, 20) anfont:12];
    UIButton *rithtItem=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, size.width+20, 20)];
    rithtItem.layer.masksToBounds=YES;
    rithtItem.layer.borderColor=[UIColor whiteColor].CGColor;
    rithtItem.layer.borderWidth=.7;
    rithtItem.layer.cornerRadius=2.0;
    [rithtItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rithtItem.titleLabel.font=[UIFont systemFontOfSize:12];
    [rithtItem setTitle:title forState:UIControlStateNormal];
    [rithtItem setImage:[UIImage imageNamed:@"white_angle"] forState:UIControlStateNormal];
    [rithtItem verticalImageAndTitle:size.width-5 andImage:[UIImage imageNamed:@"white_angle"]];
    
    [rithtItem addTarget:self action:@selector(onclickRight:) forControlEvents:UIControlEventTouchUpInside];
    return rithtItem;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
